﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DosciWordPlugin.Common;
using DosciWordPlugin.Models;
using System.IO;

namespace DosciWordPlugin.Controls
{
    public partial class WordTemplates : Form
    {
        public TemplateModel SelectedTemplate { get; set; }

        public WordTemplates()
        {
            InitializeComponent();
        }

        private void btnGetRemote_Click(object sender, EventArgs e)
        {
            ReloadRemoteTemplate();
        }

        private void btnDownload_Click(object sender, EventArgs e)
        {
            try
            {
                if (listViewRemote.SelectedItems == null || listViewRemote.SelectedItems.Count <= 0)
                {
                    WinApp.MsgBoxInfo("请选择要下载的模板");
                    return;
                }

                Cursor = Cursors.WaitCursor;
                btnDownload.Text = "下载中...";
                btnDownload.Enabled = false;

                bool downloaded = false;
                foreach (ListViewItem item in listViewRemote.SelectedItems)
                {
                    TemplateModel model = item.Tag as TemplateModel;
                    if (model == null || CacheManager.TemplateList.Exists(t=>t.ID==model.ID)) continue;
                    
                    try
                    {
                        string localSaveFilePath = CacheManager.DATA_FOLDER + model.ID + "___" + model.Name + ".dot";

                        Dictionary<string, string> dicParam = new Dictionary<string, string>();
                        dicParam.Add("id", model.ID);

                        HttpUtil.DownloadFile(model.Path, dicParam, localSaveFilePath);

                        if (File.Exists(localSaveFilePath))
                        {
                            model.LocalFilePath = localSaveFilePath;
                            item.SubItems[1].Text = "已下载";
                            CacheManager.TemplateList.Add(model);
                        }
                    }
                    catch (Exception ex)
                    {
                        LogHelper.Error(ex);
                    }
                    finally
                    {
                        downloaded = true;
                    }
                }
                if (downloaded)
                    ReloadLocalTemplate();
                if (!downloaded) WinApp.MsgBoxInfo("请选择要下载的模板");
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
            }
            finally {

                Cursor = Cursors.Arrow;
                btnDownload.Text = "下载到本地";
                btnDownload.Enabled = true;
            }
        }

        private void btnApply_Click(object sender, EventArgs e)
        {
            try
            {
                if (listViewLocal.SelectedItems == null || listViewLocal.SelectedItems.Count <= 0) 
                {
                    WinApp.MsgBoxInfo("请选择要应用的模板");
                    return;
                }

                SelectedTemplate = listViewLocal.SelectedItems[0].Tag as TemplateModel;
                DialogResult = System.Windows.Forms.DialogResult.OK;
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            try
            {
                DialogResult = System.Windows.Forms.DialogResult.Cancel;
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
            }
        }

        private void WordTemplates_Shown(object sender, EventArgs e)
        {
            ReloadRemoteTemplate();
            ReloadLocalTemplate();
        }

        private void ReloadRemoteTemplate() 
        {
            try
            {
                btnGetRemote.Text = "拉取中...";
                btnGetRemote.Enabled = false;
                Cursor = Cursors.WaitCursor;

                List<TemplateModel> templateList = HttpUtil.Post<List<TemplateModel>>(Constant.API_模板列表, null);

                if (templateList == null) return;

                this.listViewRemote.BeginUpdate();
                listViewRemote.Items.Clear();
                foreach (var remoteTemplete in templateList)
                {
                    ListViewItem lvi = new ListViewItem();
                    lvi.ImageIndex = 0;
                    lvi.Text = remoteTemplete.Name;
                    lvi.SubItems.Add(CacheManager.TemplateList.Exists(t => t.ID == remoteTemplete.ID) ? "已下载" : "未下载");
                    lvi.Tag = remoteTemplete;
                    this.listViewRemote.Items.Add(lvi);
                }
                this.listViewRemote.EndUpdate();
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                WinApp.MsgBoxError(ex.Message);
            }
            finally
            {
                btnGetRemote.Text = "获取远程";
                btnGetRemote.Enabled = true;
                Cursor = Cursors.Arrow;
            }
        }

        private void ReloadLocalTemplate()
        {
            try
            {
                this.listViewLocal.BeginUpdate();
                this.listViewLocal.Items.Clear();
                foreach (var localTemplate in CacheManager.TemplateList)
                {
                    ListViewItem lvi = new ListViewItem();
                    lvi.ImageIndex = 0;
                    lvi.Text = localTemplate.Name;
                    lvi.Tag = localTemplate;
                    this.listViewLocal.Items.Add(lvi);
                }

                this.listViewLocal.EndUpdate();
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                WinApp.MsgBoxError(ex.Message);
            }
        }
    }
}
